package com.example.libraryManager.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.libraryManager.entity.User;
import com.example.libraryManager.form.UserForm;
import com.example.libraryManager.service.impl.UserService;
import com.example.libraryManager.utils.ResultUtils;
import com.example.libraryManager.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2022-09-02
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户 登录
     * @param user
     * @return
     */
    @GetMapping ("/login")
    public ResultVo login(UserForm user){
        log.info("login=============================>:"+user);

        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("u_account",user.getUsername()).eq("u_password",user.getPassword());

        User one = userService.getOne(wrapper);
        if(one==null){
            return ResultUtils.failed(-1);
        }else {
            return ResultUtils.success(one);
        }
    }

    /**
     * 用户 更新
     * @param user
     * @return
     */
    @GetMapping("/updateUser")
    public ResultVo updateUser(User user){
        System.out.println("-------------------------------"+user);

        boolean b = userService.updateById(user);
        if (!b){
            return ResultUtils.failed(1);
        }
        return ResultUtils.success(user);
    }
}

